
export function request(shareInfo = {title: "",desc: ""}) {
  if(!getIsWxClient()) {
    return;
  }
  loadJssdk();
  var opts = Object.assign({}, {
    link: location.href,
    imgUrl: "http://liuxue.xhd.cn/lx/assets/share-icon.jpg"
  }, shareInfo);

  $.ajax({
    type: "GET",
    url: "http://liuxue.xhd.cn/wechat/getSignature",
    data: {url: `${location.href}`},
    dataType: "json",
    success: function ({appId,timestamp,nonceStr,signature
    }) {
      wechatJSDKSignature({
        appId,
        timestamp,
        nonceStr,
        signature,
        opts
      });
    },
    //请求失败，包含具体的错误信息
    error: function (e) {
      alert(e)
    }
  });
}
// request({ title: "分享标题test", desc: "分享描述test" })


function wechatJSDKSignature({
  appId,
  timestamp,
  nonceStr,
  signature,
  opts
}) {
  wx.config({
    debug: false, // 是否开启调试模式
    appId, //appid
    timestamp, // 时间戳
    nonceStr, // 随机字符串
    signature, // 签名
    jsApiList: [
      'onMenuShareTimeline',
      'onMenuShareAppMessage'
    ] // 需要使用的JS接口列表
  })

  var formateOpts = JSON.stringify(opts);

  wx.ready(function () {
    // 分享给好友
    wx.onMenuShareAppMessage(JSON.parse(formateOpts))
    // 分享到朋友圈
    wx.onMenuShareTimeline(JSON.stringify(opts))
  })
}
function getIsWxClient () {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
      return true;
  }
  return false;
};
function loadJssdk() {
  var shareSource = document.createElement('script');
  shareSource.src =
    'https://res.wx.qq.com/open/js/jweixin-1.2.0.js';
  var dom = document.getElementsByTagName('script')[0];
  dom.parentNode.insertBefore(shareSource, dom);
}
